package model;

import com.google.code.morphia.dao.BasicDAO;
import com.google.code.morphia.query.Query;
import entity.News;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.bson.types.ObjectId;

public class NewsModel extends BasicDAO<News, ObjectId> {

	public NewsModel() {
		super(DBConnection.getMongo(), DBConnection.getMorphia(), DBConnection
				.getDatabase());
		ds.ensureIndexes();
	}

	public List<News> all() {
		Query<News> q = ds.createQuery(News.class);
		q.field("complete").equals(true);
		q.field("status").equal(1);
		q.order("-dog");
		return q.limit(10).asList();
	}

	public News findById(ObjectId id) {
		return ds.find(News.class, "id", id).get();
	}

	public List<News> findByCategory(String alias) {
		Query<News> q = ds.createQuery(News.class);
		q.field("complete").equals(true);
		q.field("category").equal(alias);
		return q.asList();
	}

	public List<News> findLatestByCategory(String alias) {
		Query<News> q = ds.createQuery(News.class);
		q.field("complete").equals(true);
		q.field("category").equal(alias);
		q.order("-doc");
		return q.limit(6).asList();
	}

	public List<News> findBySubCategory(String cateAlias, String alias,
			int limit) {
		Query<News> q = ds.createQuery(News.class);
		q.field("complete").equals(true);
		q.field("category").equal(cateAlias);
		q.field("subCategory").equal(alias);
		return q.order("-dog").limit(limit).asList();
	}

	public List<News> findLatest() {
		Query<News> q = ds.createQuery(News.class);
		q.field("complete").equal(true);
		q.field("status").equal(1);
		q.order("-doc");
		return q.limit(10).asList();
	}

	public News findRelatedLink(String url) {
		Query<News> q = ds.find(News.class);
		q.field("url").equal(url);
		q.field("complete").equals(true);
		return (News) q.get();
	}

	public News findByAlias(String alias) {
		return ds.find(News.class, "alias", alias).get();
	}

	public News findByUrl(String url) {
		return ds.find(News.class, "url", url).get();
	}

	public boolean isExits(String url, String alias) {
		Query<News> q1 = ds.find(News.class);
		q1.field("url").equal(url);
		News nUrl = q1.get();
		Query<News> q2 = ds.find(News.class);
		q2.field("alias").equal(alias);
		News nAlias = q2.get();
		if (nAlias != null || nUrl != null)
			return true;
		return false;
	}

	public News getFalseComplete(String source) {
		Query<News> q = ds.find(News.class);
		q.field("complete").equal(false);
		q.field("status").equal(0);
		q.field("source").equal(source);
		q.order("-index");
		News result = (News) q.get();
		if (result != null) {
			result.setStatus(2);
			save(result);
		}
		return result;
	}

	public List<News> findByTag(String tag) {
		Query<News> q = ds.createQuery(News.class);
		q.field("tagged").equal(tag);
		q.field("status").equal(1);
		q.field("complete").equal(true);
		q.order("-doc");
		return q.limit(10).asList();
	}

	public List<News> findLastWeek(String cate, String sub) {
		Calendar cal = Calendar.getInstance();
		int date = cal.get(Calendar.DATE);
		int month = cal.get(Calendar.MONTH);
		int year = cal.get(Calendar.YEAR);
		// get exactly last week.
		if (date > 7) {
			date = date - 7;
		} else {
			date = 28 + (date - 7);
			if (month == 0) {
				month = 11;
				year = year - 1;
			} else {
				month = month - 1;
			}
		}
		cal.set(year, month, date);
		System.out.println(cal.getTime());
		Query<News> q = ds.createQuery(News.class);
		q.field("complete").equal(true);
		q.field("status").equal(1);
		if (cate != null) {
			q.field("category").equal(cate);
		}
		if (sub != null) {
			q.field("subCategory").equal(sub);
		}
		q.field("doc").lessThan(cal.getTime());
		q.order("-doc");
		return q.limit(10).asList();
	}

	public List<News> findLastMonth(String cate, String sub) {
		Calendar cal = Calendar.getInstance();
		int date = cal.get(Calendar.DATE);
		int month = cal.get(Calendar.MONTH);
		int year = cal.get(Calendar.YEAR);
		// get exactly last week.
		if (month == 0) {
			month = 11;
			year = year - 1;
		} else {
			month = month - 1;
		}
		cal.set(year, month, date);
		System.out.println(cal.getTime());
		Query<News> q = ds.createQuery(News.class);
		q.field("complete").equal(true);
		q.field("status").equal(1);
		if (cate != null) {
			q.field("category").equal(cate);
		}
		if (sub != null) {
			q.field("subCategory").equal(sub);
		}
		q.field("doc").lessThan(cal.getTime());
		q.order("-doc");
		return q.limit(10).asList();
	}

	public List<News> findByDay(String cate, Calendar cal) {
		Calendar tempCal = Calendar.getInstance();
		Query<News> q = ds.createQuery(News.class);
		q.field("complete").equal(true);
		q.field("status").equal(1);
		if (cate != null) {
			q.field("category").equal(cate);
		}
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		int day = cal.get(Calendar.DATE);
		tempCal.set(year, month - 1, day, 00, 00, 00);
		System.out.println("> " + tempCal.getTime());
		q.field("doc").greaterThanOrEq(tempCal.getTime());
		tempCal.set(year, month - 1, day + 1, 00, 00, 00);
		System.out.println("< " + tempCal.getTime());
		q.field("doc").lessThan(tempCal.getTime());
		q.order("-doc");
		return q.limit(6).asList();
	}

	public List<News> findRelatedNews(String alias) {
		News n = findByAlias(alias);
		if (n != null) {
			Query<News> q = ds.createQuery(News.class);
			q.field("complete").equal(true);
			q.field("status").equal(1);
			q.field("category").equal(n.getCategory());
			q.field("subCategory").equal(n.getSubCategory());
			q.field("doc").lessThan(n.getDoc());
			q.order("-doc");
			return q.limit(10).asList();
		} else {
			return new ArrayList<News>();
		}

	}

	public List<News> findTopNews() {
		Query<News> q = ds.createQuery(News.class);
		q.field("complete").equal(true);
		q.field("status").equal(1);
		q.order("-countView");
		return q.limit(10).asList();
	}

	public static void main(String[] args) {
		NewsModel model = new NewsModel();
		News n = model.getFalseComplete("dantri.com.vn");
		System.out.println(n.getUrl());
	}
}
